package ch2linkedlist;

import java.util.Iterator;
import java.util.LinkedHashMap;

import static ch2linkedlist.Chapter.*;

public class Ex1 {
	static ListNode removeDups(ListNode head) {
		LinkedHashMap<Integer, ListNode> set = new LinkedHashMap<>();
		ListNode cursor = head;
		while (cursor != null) {
			if (!set.containsKey(cursor.val)) {
				set.put(cursor.val, cursor);
			}
			cursor = cursor.next;
		}
		
		Iterator<ListNode> iter = set.values().iterator();
		ListNode newHead = iter.next();
		ListNode curr = newHead;
		while (iter.hasNext()) {
			ListNode next = iter.next();
			curr.next = next;
			curr = next;
		}
		curr.next = null;
		
		return newHead;
	}
	
	public static void main(String ...strings) {
		ListNode head = createSampleList(new int [] {1, 2, 1, 3, 2, 5, 2});
		print(head);
		ListNode removeDups = removeDups(head);
		print(removeDups);
		int x=0;
	}
}
